package com.oceansoft.core.rcgz.controller;

import com.hui.platform.system.exception.HuiException;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.domain.Signin;
import com.oceansoft.core.jxkh.service.ISigninService;
import com.oceansoft.core.rcgz.domain.Dailywork;
import com.oceansoft.core.rcgz.service.IRcgzService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by OS on 2017/12/1.
 */

@Controller
@RequestMapping("rcwork")
public class RcController {
    @Resource
    private ISigninService signinService;

    @Resource
    private IRcgzService rcgzService;

    @RequestMapping(value="dailyWorkInfo1.html")
    public String dailyWorkInfo1(HttpServletRequest request, ModelMap modelMap) {
        String workdate = request.getParameter("workdate");
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        Dailywork dailywork = new Dailywork();
        Signin signin = new Signin();
        try {
            signin = signinService.findByPoliceNumAndDate(user.getPoliceNum(),workdate);
            dailywork = rcgzService.dailyWorkContent(user.getPoliceNum(),workdate);
        } catch (HuiException e) {
            e.printStackTrace();
        }
        modelMap.addAttribute("work1",dailywork.getContent1());
        modelMap.addAttribute("work2",dailywork.getContent2());
        modelMap.addAttribute("work3",dailywork.getContent3());
        modelMap.addAttribute("signin",signin);
        modelMap.addAttribute("workdate",workdate);
        modelMap.addAttribute("dbid",dailywork.getDbid());
        return "dailyWork/edit";
    }

    @RequestMapping(value="saveWorkInfo", method = POST)
    public void saveWorkInfo(HttpServletRequest request, Dailywork dailywork, ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
        dailywork.setPoliceNum(user.getPoliceNum());
        try{
            if (dailywork.getDbid().intValue() == Integer.MIN_VALUE) {
                rcgzService.addWorkContent(dailywork);
            } else {
                rcgzService.editWokrConent(dailywork);
            }
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }
}
